package com.azxc.rapid.plus.system.service.impl;

import com.azxc.rapid.core.mp.base.BaseServiceImpl;
import com.azxc.rapid.core.tool.utils.BeanUtil;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.plus.system.entity.AppRoleMenuEntity;
import com.azxc.rapid.plus.system.excel.AppRoleMenuExcel;
import com.azxc.rapid.plus.system.mapper.AppRoleMenuMapper;
import com.azxc.rapid.plus.system.service.IAppRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * APP角色菜单表 服务实现类
 */
@Service
public class AppRoleMenuServiceImpl extends BaseServiceImpl<AppRoleMenuMapper, AppRoleMenuEntity> implements IAppRoleMenuService {
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importAppRoleMenu(List<AppRoleMenuExcel> data, Boolean isCovered) {
		data.forEach(appRoleMenuExcel -> {
			AppRoleMenuEntity appRoleMenu = Objects.requireNonNull(BeanUtil.copy(appRoleMenuExcel, AppRoleMenuEntity.class));
			this.submit(appRoleMenu);
		});
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean submit(AppRoleMenuEntity entity) {
		return save(entity);
	}

	@Override
	public List<AppRoleMenuExcel> exportAppRoleMenu(Wrapper<AppRoleMenuEntity> queryWrapper) {
		List<AppRoleMenuExcel> appRoleMenuList = baseMapper.exportAppRoleMenu(queryWrapper);
		return appRoleMenuList;
	}

	@Override
	public List<String> roleMenu(String roleId) {
		AppRoleMenuEntity appRoleMenuEntity = baseMapper.roleMenu(Func.toLongList(roleId));
		List<String> list = new ArrayList<>();
		String[] split = Func.split(appRoleMenuEntity.getMenuId(), ",");
		for (String s : split) {
			list.add(s);
		}
		return list;
	}
}
